package com.yun.algorithmproblem.leetcode;

public class Leetcode80 {

    public int removeDuplicates(int[] nums) {
        int p = 1, cnt = 1;
        int prev = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == prev) {
                if (cnt < 2) {
                    swap(nums, p, i);
                    p++;
                }
                cnt++;
            } else {
                cnt = 1;
                prev = nums[i];
                swap(nums, p, i);
                p++;
            }
        }
        return p;
    }

    private void swap(int[] nums, int x, int y) {
        if (x == y) {
            return;
        }
        int temp = nums[x];
        nums[x] = nums[y];
        nums[y] = temp;
    }
}
